package com.flyone.herbal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyone.herbal.common.BaseResponse;
import com.flyone.herbal.common.ResultUtils;
import com.flyone.herbal.model.dto.medicine.MedicineQueryRequest;
import com.flyone.herbal.model.dto.tcminfo.TCMQueryRequest;
import com.flyone.herbal.model.entity.ChineseMedicinalFormulae;
import com.flyone.herbal.model.entity.ChineseMedicine;
import com.flyone.herbal.service.ChineseMedicinalFormulaeService;
import com.flyone.herbal.service.ChineseMedicineService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 药方 控制类
 *
 * @author flyone
 * @date 2024/4/20
 */
@RestController
@RequestMapping("/medicine")
public class MedicineController {

    @Resource
    private ChineseMedicineService chineseMedicineService;

    @Resource
    private ChineseMedicinalFormulaeService chineseMedicinalFormulaeService;

    @PostMapping("/info")
    public BaseResponse<Page<ChineseMedicine>> getTCMName(@RequestBody TCMQueryRequest tcmQueryRequest) {
        long current = tcmQueryRequest.getCurrent();
        long size = tcmQueryRequest.getPageSize();
        Page<ChineseMedicine> page = chineseMedicineService.page(new Page<>(current, size));
        return ResultUtils.success(page);
    }

    @GetMapping("/province/{province}")
    public BaseResponse<List<ChineseMedicine>> getTCMNameByProvince(@PathVariable String province) {
        List<ChineseMedicine> tcmList = chineseMedicineService.getTCMNameByProvince(province);
        return ResultUtils.success(tcmList);
    }

    @GetMapping
    public BaseResponse<ChineseMedicine> getMedicineById(@RequestParam("id") Long id) {
        ChineseMedicine chineseMedicine = chineseMedicineService.getById(id);
        return ResultUtils.success(chineseMedicine);
    }

    @PostMapping("/list/page")
    public BaseResponse<Page<ChineseMedicinalFormulae>> getMedicines(@RequestBody MedicineQueryRequest medicineQueryRequest) {
        long current = medicineQueryRequest.getCurrent();
        long size = medicineQueryRequest.getPageSize();
        Page<ChineseMedicinalFormulae> page = chineseMedicinalFormulaeService.page(new Page<>(current, size));
        return ResultUtils.success(page);
    }

    @GetMapping("/{id}")
    public BaseResponse<ChineseMedicinalFormulae> getMedicinalFormulaeById(@PathVariable Long id) {
        ChineseMedicinalFormulae chineseMedicinalFormulae = chineseMedicinalFormulaeService.getById(id);
        return ResultUtils.success(chineseMedicinalFormulae);
    }

    @PostMapping("/search")
    public BaseResponse<Page<ChineseMedicinalFormulae>> searchMedicines(@RequestBody MedicineQueryRequest medicineQueryRequest) {
        QueryWrapper<ChineseMedicinalFormulae> queryWrapper = chineseMedicinalFormulaeService.getQueryWrapper(medicineQueryRequest);
        long current = medicineQueryRequest.getCurrent();
        long size = medicineQueryRequest.getPageSize();
        Page<ChineseMedicinalFormulae> medicineList = chineseMedicinalFormulaeService.page(new Page<>(current, size), queryWrapper);;
        return ResultUtils.success(medicineList);
    }
}
